<template>
  <div class="setting-container">
    <el-card>
      <!-- 导航 -->
      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
          <el-breadcrumb-item>个人设置</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 栅格 -->
      <el-row>
        <el-col :span="12">
          <!-- 表单 -->
          <el-form label-width="120px">
            <el-form-item label="手机：">{{ userInfo.mobile }}</el-form-item>
            <el-form-item label="媒体名称：">
              <el-input v-model="userInfo.name"></el-input>
            </el-form-item>
            <el-form-item label="媒体简介：">
              <el-input
                v-model="userInfo.intro"
                type="textarea"
                :rows="3"
              ></el-input>
            </el-form-item>
            <el-form-item label="联系邮箱：">
              <el-input v-model="userInfo.email"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="updateUser" type="primary">保存设置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <!-- 上传组件 action必须属性-->
          <el-upload
            class="avatar-uploader"
            action=""
            :http-request="uploadPhoto"
            :before-upload="beforeAvatarUpload"
            :on-success="handleAvatarSuccess"
            :show-file-list="false"
          >
            <el-image
              class="avatar"
              v-if="userInfo.photo"
              :src="userInfo.photo"
              fit="cover"
            ></el-image>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="edit-photo">修改头像</p>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { getProfile, modUserProfile, moduserImage } from '@/api/user'
// 默认示例头像
const defImg = 'https://gitee.com/mengi/imgs/raw/master/img/timg.jpeg'
export default {
  name: 'my-setting',
  data () {
    return {
      // 用户信息
      userInfo: {
        name: 'test',
        intro: 'info',
        email: 'info@qq.com',
        mobile: '',
        id: '',
        photo: defImg
      },
      // 支持上传组件的数据
      imageUrl: null
    }
  },
  created () {
    this.getProfile()
  },
  methods: {
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 获取个人信息
    async getProfile () {
      const { data: { data } } = await getProfile()
      this.userInfo = data
    },
    // 更新个人信息
    async updateUser () {
      const { name, intro, email } = this.userInfo
      try {
        await modUserProfile({ name, intro, email })
        this.eventBus.$emit('setUserName', name)
        this.$message.success('修改成功！')
      } catch (error) {
        this.$message.error('修改失败！')
      }
    },
    handleAvatarSuccess (res) {
      console.log('sc:', res)
    },
    // 更新用户头像
    async uploadPhoto (fileObj) {
      // console.log(fileObj, moduserImage)
      // 获取上传的文件对象
      const { file } = fileObj
      // 处理上传需要的FormData对象
      const fd = new FormData()
      fd.append('photo', file)
      try {
        const { data: { data } } = await moduserImage(fd)
        this.eventBus.$emit('setUserPhoto', data.photo)
        // 跟新本地头像
        this.userInfo.photo = data.photo
        this.$message.success('上传成功！')
      } catch (error) {
        this.$message.error('上传失败！')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.avatar {
  width: 178px;
  height: 178px;
  vertical-align: middle;
}
.avatar-uploader {
  text-align: center;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  &:hover {
    border-color: rgb(255, 208, 75);
  }
}
.edit-photo {
  font-size: 12px;
  text-align: center;
}
</style>
